package com.atguigu;

import com.ververica.cdc.connectors.mysql.MySqlSource;
import com.ververica.cdc.connectors.mysql.table.StartupOptions;
import com.ververica.cdc.debezium.DebeziumSourceFunction;
import com.ververica.cdc.debezium.StringDebeziumDeserializationSchema;
import org.apache.flink.runtime.state.filesystem.FsStateBackend;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;

public class DataStreamApi3WithDeser {

    public static void main(String[] args) throws Exception {

        System.setProperty("HADOOP_USER_NAME", "atguigu");

        //TODO 1.获取执行环境
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        env.setParallelism(1);

        env.setStateBackend(new FsStateBackend("hdfs://hadoop102:8020/flinkcdc/210927"));
        env.enableCheckpointing(5000L);
        //env.getCheckpointConfig().enableExternalizedCheckpoints(CheckpointConfig.ExternalizedCheckpointCleanup.RETAIN_ON_CANCELLATION);

        //TODO 2.使用FlinkCDC创建SourceFunction
        DebeziumSourceFunction<String> sourceFunction = MySqlSource.<String>builder()
                .hostname("hadoop102")
                .port(3306)
                .username("root")
                .password("000000")
                .databaseList("gmall-210927-flink")
                .tableList("gmall-210927-flink.base_trademark") //注意：写表的时候一定要带上库名
                .deserializer(new StringDebeziumDeserializationSchema())
                //earliest:需要再创建数据库之前就开启Binlog
                .startupOptions(StartupOptions.initial())
                .build();

        //TODO 3.读取数据
        DataStreamSource<String> dataStream = env.addSource(sourceFunction);

        //TODO 4.打印数据
        dataStream.print();

        //TODO 5.启动任务
        env.execute();

    }

}
